
capt log close _all
log using   "${logs}Figure2" , replace text
*------------

// Time to run: around 30 seconds

*==================================================================
// Figure 2: BEV share by local incentives and household type, 2015-2017
*==================================================================

*=========================================
// Step 1: couples
*=========================================

use    "${dataout}MainDataset" , clear
keep if year>=2015 & year<=2017
keep if couple==1
keep couple toll_fam_mean BEV_fam_yes familienr grkrets_num grk_bed1_num grk_bed2_num ptl_fam_km_mean


*-----------------------------------
// Figure 2a: road tolls, couples
*-----------------------------------

// Construct intervals for bins
gen toll_fam_mean_int=.
replace toll_fam_mean_int=0 if toll_fam_mean==0
local count=0
dis `count'
forvalues i=0(5)45 {
local count = `count' + 5
dis `count'
replace toll_fam_mean_int=`count' if toll_fam_mean>`i' & toll_fam_mean<=`count'
}
replace toll_fam_mean_int=50 if toll_fam_mean>=50 & toll_fam_mean!=.

bysort toll_fam_mean_int: egen toll_bin=mean(toll_fam_mean)
bysort toll_fam_mean_int: egen BEV_bin=mean(BEV_fam_yes)
bysort toll_fam_mean_int: egen toll_bin_count=count(familienr)

// Add equation from lfit
capt gen helpvar=1
reghdfe BEV_fam_yes toll_fam_mean , absorb(helpvar)  vce(cluster grkrets_num grk_bed1_num grk_bed2_num) 
eststo
drop helpvar 

local coeff _b[toll_fam_mean]
local fmtcoeff : dis %7.5f `coeff'
dis `fmtcoeff'

local se _se[toll_fam_mean]
local fmtse : dis %7.6f `se'
dis `fmtse'

twoway ///
(scatter BEV_bin toll_fam_mean_int [w=toll_bin_count] if toll_fam_mean_int<=50, msymbol(circle_hollow) mcolor(navy) ) ///
(lfit BEV_fam_yes toll_fam_mean if toll_fam_mean<=50 , lcolor(navy) estopts(beta) ) ///
, ytitle("Household owns {&ge}1 BEV (0/1)") ylab(0(0.05)0.25)    ///
xtitle("Road toll (NOK)") xlab(0(5)50) ///
graphregion(color(white))  yscale(titlegap(1.5)) xscale(titlegap(1.5))  ///
legend(off) scale(1.3) ///
text(0.022 44  "Coeff: `fmtcoeff'")  ///
text(0.007 46  "(`fmtse')")  


graph export     "${figures}Figure2a.png" , replace
graph export     "${figures}Figure2a.pdf" , replace


*-----------------------------------
// Figure 2c: bus lane distance, couples
*-----------------------------------

// Construct intervals for bins
gen ptl_fam_km_mean_int=.
replace ptl_fam_km_mean_int=0 if ptl_fam_km_mean==0
local count=0
dis `count'
forvalues i=0(0.5)4.5 {
local count = `count' + 0.5
dis `count'
replace ptl_fam_km_mean_int=`count' if ptl_fam_km_mean>`i' & ptl_fam_km_mean<=`count'
}
replace ptl_fam_km_mean_int=5 if ptl_fam_km_mean>=5 & ptl_fam_km_mean!=.

bysort ptl_fam_km_mean_int: egen ptl_bin=mean(ptl_fam_km_mean)
bysort ptl_fam_km_mean_int: egen BEV_ptl_bin=mean(BEV_fam_yes)
bysort ptl_fam_km_mean_int: egen ptl_bin_count=count(familienr)

// Add equation from lfit
capt gen helpvar=1
reghdfe BEV_fam_yes ptl_fam_km_mean , absorb(helpvar)  vce(cluster grkrets_num grk_bed1_num grk_bed2_num) 
eststo
drop helpvar 

local coeff _b[ptl_fam_km_mean]
local fmtcoeff : dis %7.5f `coeff'
dis `fmtcoeff'

local se _se[ptl_fam_km_mean]
local fmtse : dis %7.6f `se'
dis `fmtse'

twoway ///
(scatter BEV_ptl_bin ptl_fam_km_mean_int [w=ptl_bin_count] if ptl_fam_km_mean_int<=5, msymbol(circle_hollow) mcolor(navy) ) ///
(lfit BEV_fam_yes ptl_fam_km_mean if ptl_fam_km_mean<=5 , lcolor(navy) estopts(beta) ) ///
, ytitle("Household owns {&ge}1 BEV (0/1)") ylab(0(0.05)0.25)    ///
xtitle("Bus lane (km)") xlab(0(1)5) ///
graphregion(color(white))  yscale(titlegap(1.5)) xscale(titlegap(1.5))  ///
legend(off) scale(1.3) ///
text(0.022 4.4  "Coeff: `fmtcoeff'")  ///
text(0.007 4.6  "(`fmtse')")  

graph export  "${figures}Figure2c.pdf" ,  replace
graph export  "${figures}Figure2c.png" ,  replace
//graph save    "${figures}Figure2c.gph" ,  replace





*=========================================
// Step 2: single-adult HH
*=========================================

use    "${dataout}MainDataset" , clear
keep if year>=2015 & year<=2017
keep if couple==0
keep couple toll_fam_mean BEV_fam_yes familienr grkrets_num grk_bed1_num grk_bed2_num ptl_fam_km_mean


/********** Generate one value per row values ... **********************************/
egen grk_bed_num  = rowmax(grk_bed1_num  grk_bed2_num) 
/**************************************************************************************/

*-----------------------------------
// Figure 2b: road tolls, single-adult
*-----------------------------------

//intervals 
gen toll_fam_mean_int=.
replace toll_fam_mean_int=0 if toll_fam_mean==0
local count=0
dis `count'
forvalues i=0(5)45 {
local count = `count' + 5
dis `count'
replace toll_fam_mean_int=`count' if toll_fam_mean>`i' & toll_fam_mean<=`count'
}
replace toll_fam_mean_int=50 if toll_fam_mean>=50 & toll_fam_mean!=.

bysort toll_fam_mean_int: egen toll_bin=mean(toll_fam_mean)
bysort toll_fam_mean_int: egen BEV_bin=mean(BEV_fam_yes)
bysort toll_fam_mean_int: egen toll_bin_count=count(familienr)

// Add equation from lfit
capt gen helpvar=1
reghdfe BEV_fam_yes toll_fam_mean , absorb(helpvar)  vce(cluster grkrets_num grk_bed_num ) 
eststo
drop helpvar 

local coeff _b[toll_fam_mean]
local fmtcoeff : dis %7.5f `coeff'
dis `fmtcoeff'

local se _se[toll_fam_mean]
local fmtse : dis %7.6f `se'
dis `fmtse'

twoway ///
(scatter BEV_bin toll_fam_mean_int [w=toll_bin_count] if toll_fam_mean_int<=50, msymbol(circle_hollow) mcolor(navy) ) ///
(lfit BEV_fam_yes toll_fam_mean if toll_fam_mean<=50 , lcolor(navy) estopts(beta) ) ///
, ytitle("Household owns {&ge}1 BEV (0/1)") ylab(0(0.01)0.05)     ///
xtitle("Road toll (NOK)") xlab(0(5)50) ///
graphregion(color(white))  yscale(titlegap(1.5)) xscale(titlegap(1.5))  ///
legend(off) scale(1.3) ///
text(0.0045 44  "Coeff: `fmtcoeff'")  ///
text(0.001 46  "(`fmtse')")  

graph export  "${figures}Figure2b.pdf" ,  replace
graph export  "${figures}Figure2b.png" ,  replace
//graph save    "${figures}Figure2b.gph" ,  replace




*-----------------------------------
// Figure 2d: bus lane distance, single-adult
*-----------------------------------

//intervals 
gen ptl_fam_km_mean_int=.
replace ptl_fam_km_mean_int=0 if ptl_fam_km_mean==0
local count=0
dis `count'
forvalues i=0(0.5)4.5 {
local count = `count' + 0.5
dis `count'
replace ptl_fam_km_mean_int=`count' if ptl_fam_km_mean>`i' & ptl_fam_km_mean<=`count'
}
replace ptl_fam_km_mean_int=5 if ptl_fam_km_mean>=5 & ptl_fam_km_mean!=.

bysort ptl_fam_km_mean_int: egen ptl_bin=mean(ptl_fam_km_mean)
bysort ptl_fam_km_mean_int: egen BEV_ptl_bin=mean(BEV_fam_yes)
bysort ptl_fam_km_mean_int: egen ptl_bin_count=count(familienr)

// Add equation from lfit
capt gen helpvar=1
reghdfe BEV_fam_yes ptl_fam_km_mean , absorb(helpvar)  vce(cluster grkrets_num grk_bed_num ) 
eststo
drop helpvar 

local coeff _b[ptl_fam_km_mean]
local fmtcoeff : dis %7.5f `coeff'
dis `fmtcoeff'

local se _se[ptl_fam_km_mean]
local fmtse : dis %7.6f `se'
dis `fmtse'

twoway ///
(scatter BEV_ptl_bin ptl_fam_km_mean_int [w=ptl_bin_count] if ptl_fam_km_mean_int<=5, msymbol(circle_hollow) mcolor(navy) ) ///
(lfit BEV_fam_yes ptl_fam_km_mean if ptl_fam_km_mean<=5 , lcolor(navy) estopts(beta) ) ///
, ytitle("Household owns {&ge}1 BEV (0/1)") ylab(0(0.01)0.05)    ///
xtitle("Bus lane (km)") xlab(0(1)5) ///
graphregion(color(white))  yscale(titlegap(1.5)) xscale(titlegap(1.5))  ///
legend(off) scale(1.3) ///
text(0.0045 4.4  "Coeff: `fmtcoeff'")  ///
text(0.001 4.6  "(`fmtse')")  

graph export  "${figures}Figure2d.pdf" ,  replace
graph export  "${figures}Figure2d.png" ,  replace

*------------
log close
